package com.zimin.cloud.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zimin.cloud.mine.pojo.system.business.RoleBO;
import com.zimin.cloud.mine.pojo.system.view.RoleVO;
import com.zimin.cloud.system.mapper.RoleMapper;
import com.zimin.cloud.system.pojo.entity.Role;
import com.zimin.cloud.system.service.IRoleService;
import com.zimin.common.utils.BeanUtils;
import com.zimin.common.utils.ObjectUtils;
import com.zimin.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;

/**
 * <p>
 * 角色信息 服务实现类
 * </p>
 *
 * @author zimin
 * @since 2021-12-30 11:05
 */
@Service("roleService")
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    public RoleVO getRoleByName(String roleName) {
        return BeanUtils.coverClass(getOne(Wrappers.<Role>lambdaQuery().eq(Role::getRoleName, roleName)), RoleVO.class);
    }

    @Override
    public RoleVO getRoleByKey(String roleKey) {
        return BeanUtils.coverClass(getOne(queryWrappers(RoleBO.builder().roleKey(roleKey).build())), RoleVO.class);
    }

    /**
     * <p>查询wrappers</p>
     *
     * @param roleBO
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.zimin.cloud.system.pojo.entity.Role>
     * @since 2022-02-19 17:12:37
     */
    public LambdaQueryWrapper<Role> queryWrappers(RoleBO roleBO) {
        if (ObjectUtils.isEmpty(roleBO)) {
            return null;
        }
        return Wrappers.<Role>lambdaQuery()
                .like(StringUtils.isNotBlank(roleBO.getRoleName()), Role::getRoleName, roleBO.getRoleName())
                .eq(ObjectUtils.isNotEmpty(roleBO.getStatus()), Role::getStatus, roleBO.getStatus())
                .eq(ObjectUtils.isNotEmpty(roleBO.getRoleKey()), Role::getRoleKey, roleBO.getRoleKey())
                .orderByDesc(Role::getCreateTime);

    }
}
